import * as ItemModel from "../model/textItem.js";

export const getAll = async (req, res, db) => {
  const tasks = await ItemModel.getAllItems(db);
  res.json(tasks);
};

// 分页获取文本项
export const getPagedItems = async (req, res, db) => {
  try {
    // 从查询参数获取页码和每页大小，设置默认值
    const page = parseInt(req.query.page) || 1;
    const pageSize = parseInt(req.query.pageSize) || 10;
    const keyWord = req.query.keyWord || "";

    const items = await ItemModel.getTextItemPaged(db, page, pageSize, keyWord);
    const totalItems = await ItemModel.getTextItemCount(db);
    const totalPages = Math.ceil(totalItems / pageSize);

    // 返回分页数据和元信息
    res.json({
      success: true,
      data: items,
      pagination: {
        page,
        pageSize,
        totalItems,
        totalPages,
        hasNext: page < totalPages,
        hasPrev: page > 1,
      },
    });
  } catch (error) {
    res.status(500).json({
      success: false,
      message: "获取分页数据失败",
      error: error.message,
    });
  }
};

// 创建文本项
export const createItem = async (req, res, db) => {
  try {
    const { title, description } = req.body;
    const result = await ItemModel.createTextItem(db, title, description);
    res.json({
      success: true,
      data: result,
      message: "文本项创建成功",
    });
  } catch (error) {
    res.status(500).json({
      success: false,
      message: "创建文本项失败",
      error: error.message,
    });
  }
};

// 删除文本项（硬删除）
export const deleteItem = async (req, res, db) => {
  try {
    const { id } = req.params;
    await ItemModel.deleteTextItem(db, id);
    res.json({
      success: true,
      message: "文本项删除成功",
    });
  } catch (error) {
    res.status(500).json({
      success: false,
      message: "删除文本项失败",
      error: error.message,
    });
  }
};

// 软删除文本项
export const softDeleteItem = async (req, res, db) => {
  try {
    const { id } = req.params;
    await ItemModel.softDeleteTextItem(db, id);
    res.json({
      success: true,
      message: "文本项已标记为删除",
    });
  } catch (error) {
    res.status(500).json({
      success: false,
      message: "软删除文本项失败",
      error: error.message,
    });
  }
};

// 更新文本项
export const updateItem = async (req, res, db) => {
  try {
    const { id } = req.params;
    const { title, description } = req.body;
    await ItemModel.updateTextItem(db, id, title, description);
    res.json({
      success: true,
      message: "文本项更新成功",
    });
  } catch (error) {
    res.status(500).json({
      success: false,
      message: "更新文本项失败",
      error: error.message,
    });
  }
};

// 根据ID获取单个文本项
export const getItemById = async (req, res, db) => {
  try {
    const { id } = req.params;
    const item = await ItemModel.getTextItemById(db, id);

    if (!item) {
      return res.status(404).json({
        success: false,
        message: "未找到该文本项",
      });
    }

    res.json({
      success: true,
      data: item,
    });
  } catch (error) {
    res.status(500).json({
      success: false,
      message: "获取文本项失败",
      error: error.message,
    });
  }
};
